package movEat.Kernel.DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class ManagedConnection {


	private Connection connection = null;
	private Statement statement = null;
	
	private boolean isAvailable;

	private static String url = "jdbc:mysql://localhost:";
	private static String dbName = "imdb";
	private static String driver = "com.mysql.jdbc.Driver";
	private static String userName = "root"; 
	private static String password = "85yaaronet";


	ManagedConnection() throws SQLException{

		try {
			Class.forName(driver).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		connection = DriverManager.getConnection(url+dbName, userName, password);
		statement = connection.createStatement();
						
	}

	boolean isAvailable() {
		return isAvailable;
	}

	void setAvailability(boolean availability){
		isAvailable = availability;
	}

	public Connection getConnection() {
		return connection;
	}

	Statement getStatement() {
		return statement;
	}

	PreparedStatement prepareStatement(String sql) throws SQLException{
		return connection.prepareStatement(sql);
	}
	
	void setAutoCommit(boolean autoCommit) throws SQLException{
		connection.setAutoCommit(autoCommit);
	}
	
	void commit() throws SQLException{
		connection.commit();
	}
	void shutDown() throws SQLException {
		statement.close();
		connection.close();
		
	}
	
	static void setDBUserName(String dbUserName){
		userName = dbUserName;
	}

	static void setDBPassword(String dbPassword){
		password = dbPassword;
	}

	public static void setDbName(String dbName) {
		ManagedConnection.dbName = dbName;
	}

	

	public static void setPort(String port) {
		url += port+"/";
	}
}
